package com.sakuraio.nextj.core.protocol;

import com.fasterxml.jackson.annotation.JsonAlias;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>PageRequest</p>
 *
 * @author nekoimi 2023/03/15
 */
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "分页请求参数")
public class Page implements Serializable {
    /**
     * 默认分页对象
     */
    public static final Page DEFAULT_PAGE = new Page();

    @Schema(description = "分页页码，默认值1，分页查询用")
    @JsonAlias({"page", "pageNum", "page_num", "page_index", "pageIndex"})
    private Long pageNum = 1L;

    @Schema(description = "分页每页显示数量，默认值10，分页查询用")
    @JsonAlias({"size", "pageSize", "page_size"})
    private Long pageSize = 10L;

    @Schema(description = "排序设置")
    private List<Order> orders = new ArrayList<>();

    @Schema(description = "列表排序字段")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Order implements Serializable {
        @Schema(description = "排序字段")
        private String field;

        @Schema(description = "是否按照升序排序(从小到大的顺序)，默认按照asc排")
        private Boolean asc = true;
    }

    public Long getPageNum() {
        pageNum = Optional.ofNullable(pageNum).orElse(1L);
        return pageNum <= 0 ? 1 : pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = Optional.ofNullable(pageNum).orElse(1L);
    }

    public Long getPageSize() {
        pageSize = Optional.ofNullable(pageSize).orElse(10L);
        return pageSize <= 0 ? 10 : pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = Optional.ofNullable(pageSize).orElse(10L);
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }
}
